﻿using FreeSql.Internal.Model.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sys_Print.Common
{
    public class StringToIntListHandler : ITypeHandler
    {
        public Type Type => throw new NotImplementedException();

        public object Deserialize(object value)
        {
            if (value is string stringValue)
            {
                var stringArray = stringValue.Split(',');
                var intList = new List<int>();

                foreach (var str in stringArray)
                {
                    if (int.TryParse(str, out int intValue))
                    {
                        intList.Add(intValue);
                    }
                }

                return intList;
            }

            return null;
        }

        public object Serialize(object value)
        {
            if (value is List<int> intList)
            {
                return string.Join(",", intList);
            }

            return null;
        }
    }
}
